/************************************************************************************/
/*  Program name CAPFAIMP_MASS_TXF.sql                                  			*/
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 18 JAN, 2006   NIP Karen         First created                               	*/
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPFAIMP_MASS_TXF';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_FA_MASS_TRANSFERS;
	
	lv_Next_Seq_ID		NUMBER;
	lv_COA_ID			NUMBER;

BEGIN
	UPDATE
		FA_MASS_TRANSFERS
	SET	
		BOOK_TYPE_CODE = CAPBRIMP_MAPPING.RenameStr(SUBSTR(BOOK_TYPE_CODE,1,4), BOOK_TYPE_CODE)
	WHERE BOOK_TYPE_CODE IN
		(
		SELECT 
			BOOK_TYPE_CODE
		FROM 
			CRST_FA_MASS_TRANSFERS
		);
	
	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	FA_MASS_TRANSFERS_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT	INTO	CRST_FA_MASS_TRANSFERS_MAP
			VALUES
			(
			C1.MASS_TRANSFER_ID,   
			lv_Next_Seq_ID
			);
			
			lv_COA_ID := CAPBRIMP_COMMON.GetCOAIDfromBTC(C1.Book_Type_Code);
		
			INSERT INTO FA_MASS_TRANSFERS
			VALUES
			(
			lv_Next_Seq_ID,
			C1.BOOK_TYPE_CODE,
			C1.TRANSACTION_DATE_ENTERED,
			C1.DATE_EFFECTIVE,
			C1.CONCURRENT_REQUEST_ID,
			C1.FROM_LOCATION_ID,
			C1.TO_LOCATION_ID,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.FROM_GL_CCID_D),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.TO_GL_CCID_D),
			C1.FROM_EMPLOYEE_ID,
			C1.TO_EMPLOYEE_ID,
			C1.DESCRIPTION,
			C1.ATTRIBUTE1,
			C1.ATTRIBUTE2,
			C1.ATTRIBUTE3,
			C1.ATTRIBUTE4,
			C1.ATTRIBUTE5,
			C1.ATTRIBUTE6,
			C1.ATTRIBUTE7,
			C1.ATTRIBUTE8,
			C1.ATTRIBUTE9,
			C1.ATTRIBUTE10,
			C1.ATTRIBUTE11,
			C1.ATTRIBUTE12,
			C1.ATTRIBUTE13,
			C1.ATTRIBUTE14,
			C1.ATTRIBUTE15,
			C1.ATTRIBUTE_CATEGORY_CODE,
			C1.SEGMENT1_LOW,
			C1.SEGMENT2_LOW,
			C1.SEGMENT3_LOW,
			C1.SEGMENT4_LOW,
			C1.SEGMENT5_LOW,
			C1.SEGMENT6_LOW,
			C1.SEGMENT7_LOW,
			C1.SEGMENT8_LOW,
			C1.SEGMENT9_LOW,
			C1.SEGMENT10_LOW,
			C1.SEGMENT11_LOW,
			C1.SEGMENT12_LOW,
			C1.SEGMENT13_LOW,
			C1.SEGMENT14_LOW,
			C1.SEGMENT15_LOW,
			C1.SEGMENT16_LOW,
			C1.SEGMENT17_LOW,
			C1.SEGMENT18_LOW,
			C1.SEGMENT19_LOW,
			C1.SEGMENT20_LOW,
			C1.SEGMENT21_LOW,
			C1.SEGMENT22_LOW,
			C1.SEGMENT23_LOW,
			C1.SEGMENT24_LOW,
			C1.SEGMENT25_LOW,
			C1.SEGMENT26_LOW,
			C1.SEGMENT27_LOW,
			C1.SEGMENT28_LOW,
			C1.SEGMENT29_LOW,
			C1.SEGMENT30_LOW,
			C1.SEGMENT1_HIGH,
			C1.SEGMENT2_HIGH,
			C1.SEGMENT3_HIGH,
			C1.SEGMENT4_HIGH,
			C1.SEGMENT5_HIGH,
			C1.SEGMENT6_HIGH,
			C1.SEGMENT7_HIGH,
			C1.SEGMENT8_HIGH,
			C1.SEGMENT9_HIGH,
			C1.SEGMENT10_HIGH,
			C1.SEGMENT11_HIGH,
			C1.SEGMENT12_HIGH,
			C1.SEGMENT13_HIGH,
			C1.SEGMENT14_HIGH,
			C1.SEGMENT15_HIGH,
			C1.SEGMENT16_HIGH,
			C1.SEGMENT17_HIGH,
			C1.SEGMENT18_HIGH,
			C1.SEGMENT19_HIGH,
			C1.SEGMENT20_HIGH,
			C1.SEGMENT21_HIGH,
			C1.SEGMENT22_HIGH,
			C1.SEGMENT23_HIGH,
			C1.SEGMENT24_HIGH,
			C1.SEGMENT25_HIGH,
			C1.SEGMENT26_HIGH,
			C1.SEGMENT27_HIGH,
			C1.SEGMENT28_HIGH,
			C1.SEGMENT29_HIGH,
			C1.SEGMENT30_HIGH,
			C1.SEGMENT1,
			C1.SEGMENT2,
			C1.SEGMENT3,
			C1.SEGMENT4,
			C1.SEGMENT5,
			C1.SEGMENT6,
			C1.SEGMENT7,
			C1.SEGMENT8,
			C1.SEGMENT9,
			C1.SEGMENT10,
			C1.SEGMENT11,
			C1.SEGMENT12,
			C1.SEGMENT13,
			C1.SEGMENT14,
			C1.SEGMENT15,
			C1.SEGMENT16,
			C1.SEGMENT17,
			C1.SEGMENT18,
			C1.SEGMENT19,
			C1.SEGMENT20,
			C1.SEGMENT21,
			C1.SEGMENT22,
			C1.SEGMENT23,
			C1.SEGMENT24,
			C1.SEGMENT25,
			C1.SEGMENT26,
			C1.SEGMENT27,
			C1.SEGMENT28,
			C1.SEGMENT29,
			C1.SEGMENT30,
			C1.CATEGORY_ID
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/